package com.lzxuni.modules.dataAcquisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcess;
import com.lzxuni.modules.dataAcquisition.entity.ChainLinkProcessIndex;
import com.lzxuni.modules.dataAcquisition.mapper.ChainLinkProcessIndexMapper;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessIndexService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzxuni.modules.dataAcquisition.service.ChainLinkProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 追溯链条-环节-过程-指标 服务实现类
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@Service
public class ChainLinkProcessIndexServiceImpl extends ServiceImpl<ChainLinkProcessIndexMapper, ChainLinkProcessIndex> implements ChainLinkProcessIndexService {

    @Autowired
    private ChainLinkProcessIndexMapper chainLinkProcessIndexMapper;
    @Autowired
    private ChainLinkProcessService chainLinkProcessService;

    @Override
    public PageInfo<ChainLinkProcessIndex> queryPage(PageParameter pageParameter, ChainLinkProcessIndex chainLinkProcessIndex) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<ChainLinkProcessIndex> list;
        if (StringUtils.isNotEmpty(chainLinkProcessIndex.getIndexName())) {
            list = baseMapper.selectList(new QueryWrapper<ChainLinkProcessIndex>().like("index_name", chainLinkProcessIndex.getIndexName()));
        }else {
            list = baseMapper.selectList(new QueryWrapper<>());
        }
        if (list != null && list.size() > 0) {
            for (ChainLinkProcessIndex chainLinkProcessIndex1 : list) {
                ChainLinkProcess chainLinkProcess = chainLinkProcessService.getById(chainLinkProcessIndex1.getProcessId());
                chainLinkProcessIndex1.setProcessName(chainLinkProcess == null ? null : chainLinkProcess.getProcessName());
            }
        }
        PageInfo<ChainLinkProcessIndex> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
